package com.itheima.project.knowledge;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.itheima.project.*;

import java.util.List;


public class KnowledgeAdapter extends RecyclerView.Adapter<KnowledgeAdapter.KnowledgeHolder>{

    private Context mContext;
    private List<KnowledgeTreeEntity.DataBean> mList;

    public KnowledgeAdapter(Context context, List<KnowledgeTreeEntity.DataBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public KnowledgeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.knowledge_recyclerview_item, parent, false);
        return new KnowledgeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final KnowledgeHolder holder, int position) {
        final KnowledgeTreeEntity.DataBean dataBean = mList.get(position);
        holder.tvTitle.setText(dataBean.getName());
        List<KnowledgeTreeEntity.DataBean.ChildrenBean> children = dataBean.getChildren();
        int size = children.size();
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0;i<size;i++){
            stringBuilder.append(children.get(i).getName()).append("    ");
        }
        holder.tvContent.setText(stringBuilder.toString());

        if (mlistener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mlistener.OnKnowledgeClick(holder.getAdapterPosition(),dataBean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class KnowledgeHolder extends RecyclerView.ViewHolder{

        TextView tvTitle;
        TextView tvContent;
        public KnowledgeHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title_knowledge_recyclerview_item);
            tvContent = itemView.findViewById(R.id.tv_content_knowledge_recyclerview_item);
        }
    }
    private OnKnowledgeItemListener mlistener;

    public void setOnKnowledgeItemListener(OnKnowledgeItemListener listener){
        this.mlistener = listener;
    }

    public interface OnKnowledgeItemListener{
        void OnKnowledgeClick(int position,KnowledgeTreeEntity.DataBean dataBean);
    }
}
